/**
 * Created by liaoxd on 2017/12/17.
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/sysprop/sysprop.js',
  './custom/widgets/js/yufpOrgTree.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    // 创建virtual filter model
    var vm = yufp.custom.vue({
      // TODO 请替换此id参数
      el: '#sysprop_grid',
      // 以m_开头的参数为UI数据不作为业务数据，否则为业务数据
      data: function () {
        var me = this;
        return {
          serviceUrl: backend.appCommonService + '/api/adminsmprop/',
          idView: false,
          height: yufp.custom.viewSize().height - 140,
          userId: yufp.session.userId,
          formdata: {},
          tableColumns: [
            /* { label: this.$t('sysprop.csm'), prop: 'propName', width: 200, sortable: true, resizable: true },*/
            {
              label: this.$t('sysprop.csm'),
              prop: 'title',
              sortable: true,
              resizable: true,
              template: function () {
                return '<template scope="scope">\
                                    <a href="javascipt:void(0);" class="underline" style="text-decoration:underline;" @click="_$event(\'custom-row-click\', scope)">{{ scope.row.propName }}</a>\
                                </template>';
              }
            },


            { label: this.$t('sysprop.csms'), prop: 'propDesc'},
            { label: this.$t('sysprop.csz'), prop: 'propValue'},
            { label: this.$t('sysprop.bz'), prop: 'propRemark', minWidth: 200},
            { label: this.$t('sysprop.zxbgyh'), prop: 'lastChgUsr'},
            { label: this.$t('sysprop.zxbgsj'), prop: 'lastChgDt'}
          ],

          updateFields: [{
            columnCount: 1,
            fields: [
              {
                field: 'propName',
                label: this.$t('sysprop.csm'),
                rules: [{ required: true, message: this.$t('sysprop.btx'), trigger: 'blur' }]
              },
              { field: 'propDesc', label: this.$t('sysprop.csms') },
              {
                field: 'propValue',
                label: this.$t('sysprop.csz'),
                rules: [{ required: true, message: this.$t('sysprop.btx'), trigger: 'blur' }]
              },
              {
                field: 'propRemark',
                label: this.$t('sysprop.bz'),
                type: 'textarea',
                rules: [{ max: 950, message: this.$t('sysprop.srzgc'), trigger: 'blur' }]
              }
            ]
          }],

          detailFields: [{
            columnCount: 1,
            fields: [
              { field: 'propName', label: this.$t('sysprop.csm') },
              { field: 'propDesc', label: this.$t('sysprop.csms') },
              { field: 'propValue', label: this.$t('sysprop.csz') },
              { field: 'propRemark', label: this.$t('sysprop.bz'), type: 'textarea' },
              { field: 'lastChgUsr', label: this.$t('sysprop.zxbgyh') },
              { field: 'lastChgDt', label: this.$t('sysprop.zxbgsj') }
            ]
          }],


          updateButtons: [
            {
              label: this.$t('sysprop.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model, valid) {
                if (valid) {
                  model.lastChgUsr = me.userId;
                  me.createFilter(model);
                  me.dialogVisible = false;
                  me.$refs.filterTable.remoteData();
                }
              }
            },
            {
              label: this.$t('sysprop.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model, valid) {
                if (valid) {
                  model.lastChgUsr = me.userId;
                  me.dataFliterEditFn(model);
                  me.dialogVisible = false;
                  me.$refs.filterTable.remoteData();
                }
              }
            },
            {
              label: this.$t('sysprop.qx'),
              hidden: false,
              click: function (model) {
                me.dialogVisible = false;
              }
            }
          ],
          // 表单是否显示
          dialogVisible: false,
          // 详情表单
          detaildialogVisible: false,
          // 表单是否可用
          formDisabled: false,
          // 表单操作状态
          viewType: 'DETAIL',
          // 表单多选
          selections: [],
          filterGrid: {
            // 系统参数当前行
            currentRow: null,
            // 系统参数多选ID
            multipleSelection: '',
            data: null,
            subdata: null,
            total: null,
            loading: true,
            subloading: true,

            paging: {
              page: 1,
              pageSize: 10
            },
            // 系统参数模糊查询表头
            query: {
              propDesc: '',
              propName: ''
            }

          }
        };
      },
      mounted: function () {
      },
      methods: {

        rowClickFn: function (selection, row) {
          this.selections = selection;
          // 用于单个修改
          this.filterGrid.currentRow = row;
        },
        detailFn: function (scope) {
          this.viewType = 'DETAIL';
          this.detaildialogVisible = true;

          this.$nextTick(function () {
            this.$refs.detailForm.formModel = scope.row;
          });
        },

        // 编辑按钮
        lookuptableEditFn: function () {
          if (this.$refs.filterTable.selections.length < 1) {
            vm.$message({ message: vm.$t('sysprop.qxzytjlxg') });
            return false;
          }
          if (this.$refs.filterTable.selections.length > 1) {
            vm.$message({ message: vm.$t('sysprop.znxzytjlxg') });
            return false;
          }

          this.viewType = 'UPDATE';
          vm.dialogVisible = true;
          this.$nextTick(function () {
            yufp.extend(this.$refs.datafilterForm.formModel, this.$refs.filterTable.selections[0]);
          });
        },


        // 修改系统参数
        dataFliterEditFn: function (row) {
          this.filterGrid.currentRow = row;
          var me = this;
          yufp.service.request({
            url: backend.appCommonService + '/api/adminsmprop/updates',
            method: 'post',
            data: me.filterGrid.currentRow,
            callback: function (code, message, response) {
              if (code == '0') {
                vm.$message({ message: me.$t('sysprop.xgcg') });
                vm.$refs.filterTable.remoteData();
              } else {
                vm.$message({ message: me.$t('sysprop.xgsb') });
              }
            }
          });
        },

        // 增加系统参数空行
        addFilterRecord: function () {
          this.dialogVisible = true;
          this.viewType = 'ADD';

          this.$nextTick(function () {
            this.$refs.datafilterForm.resetFields();
          });
        },

        // 保存新增系统参数
        createFilter: function (row) {
          this.filterGrid.currentRow = row;
          var me = this;
          delete this.filterGrid.currentRow.propId;
          yufp.service.request({
            url: backend.appCommonService + '/api/adminsmprop/',
            method: 'post',
            data: me.filterGrid.currentRow,
            callback: function (code, message, response) {
              if (code == '0') {
                vm.$message({ message: me.$t('sysprop.bccg') });
                vm.$refs.filterTable.remoteData();
              } else {
                vm.$message({ message: me.$t('sysprop.bcsb') });
              }
            }
          });
        },

        // 批量删除
        dataFiltermultDeleteFn: function () {
          var me = this;
          var ids = '';
          var filterSelecttions = this.$refs.filterTable.selections;
          if (filterSelecttions.length > 0) {
            for (var i = 0; i < filterSelecttions.length; i++) {
              // 记录多选用于多删
              if (filterSelecttions.length === 1) {
                ids = filterSelecttions[i].propId;
              } else {
                ids = ids + ',' + filterSelecttions[i].propId;
              }
            }
          } else {
            vm.$message({ message: vm.$t('sysprop.qxzxyscdxtcs') });
            return false;
          }
          vm.$confirm(vm.$t('sysprop.qrplscgxtcs'), vm.$t('sysprop.ts'), {
            confirmButtonText: vm.$t('sysprop.qd'),
            cancelButtonText: vm.$t('sysprop.qx'),
            type: 'warning'
          }).then(function () {
            yufp.service.request({
              url: backend.appCommonService + '/api/adminsmprop/deletes/' + ids,
              method: 'post',
              data: { ids: ids },
              callback: function (code, message, response) {
                if (code == '0') {
                  vm.$message({ message: me.$t('sysprop.sccg') });
                  vm.$refs.filterTable.remoteData();
                } else {
                  vm.$message({ message: me.$t('sysprop.scsb') });
                }
              }
            });
          });
        }


      },
      filters: {},
      watch: {
        viewType: function (value) {
          if (value == 'ADD') {
            this.updateButtons[0].hidden = false;
            this.updateButtons[1].hidden = true;
            this.detaildialogVisible = false;
          } else if (value == 'UPDATE') {
            this.updateButtons[0].hidden = true;
            this.updateButtons[1].hidden = false;
            this.detaildialogVisible = false;
          } else if (value == 'DETAIL') {
            this.detaildialogVisible = true;
          }
        }

      }
    });
  };


  // 消息处理
  exports.onmessage = function (type, message) {

  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});
